package com.app.twth.ui.tally.damagesku;

import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AlertDialog;
import androidx.recyclerview.widget.GridLayoutManager;

import com.app.twth.BR;
import com.app.twth.R;
import com.app.twth.app.AppRouter;
import com.app.twth.app.Constant;
import com.app.twth.base.BaseCameraPhotoActivity;
import com.app.twth.bean.FilePathListDTO;
import com.app.twth.bean.PhotoBean;
import com.app.twth.databinding.ActivityTallyDamageSkuBinding;
import com.app.twth.databinding.DialogTallyCheckskuBinding;
import com.app.twth.ui.checkin.PhotoAdapter;
import com.app.twth.ui.tally.Tally;
import com.app.twth.ui.tally.lpn.TallyLPNActivity;
import com.app.twth.ui.tally.sku.CheckSkuUnit;
import com.app.twth.ui.tally.sku.SkuInfo;
import com.app.twth.ui.tally.sku.SkuInfoQr;
import com.app.twth.ui.tally.sku.SkuUnitDTO;
import com.app.twth.ui.tally.sku.TallyQr;
import com.app.twth.utils.EditTextUtil;
import com.app.twth.utils.MyDialogUtil;
import com.app.twth.utils.ToastUtil;
import com.app.twth.utils.Utils;
import com.app.twth.widget.MoneyTextWatcher;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class DamageSkuActivity extends BaseCameraPhotoActivity<ActivityTallyDamageSkuBinding, DamageSkuViewModel> {
    private String TAG = getClass().getSimpleName();

    private int type = 0, index = 0, position;
    private boolean isBarCode;
    private SkuInfo skuInfo;
    private String barCode = "", sku = "";
    private Tally tally = new Tally();
    private TallyQr tallyQr;

    private String length_path, width_path, height_path, weight_path;

    private List<PhotoBean> damagePaths = new ArrayList<>();
    private PhotoAdapter damageAdapter;

    @Override
    protected String setTitle() {
        return "Damage(SKU)";
    }

    @Override
    protected int initViewModelId() {
        return BR.viewModel;
    }

    @Override
    protected void initData() {
        setNext("");
        Bundle bundle = getIntent().getBundleExtra(AppRouter.K_EXTRA);
        tally = (Tally) bundle.getSerializable("Tally");

        binding.tvContainerNo.setText(tally.getContainerNo());

        binding.tvCode.requestFocus();
        binding.tvCode.setOnEditorActionListener(EnterListenter);

        binding.tvCode.setOnFocusChangeListener(onFocusChangeListener);
        binding.tvCartons.setOnFocusChangeListener(onFocusChangeListener);


        binding.tvLength.addTextChangedListener(new MoneyTextWatcher(binding.tvLength));
        binding.tvWidth.addTextChangedListener(new MoneyTextWatcher(binding.tvWidth));
        binding.tvHeight.addTextChangedListener(new MoneyTextWatcher(binding.tvHeight));
        binding.tvWeight.addTextChangedListener(new MoneyTextWatcher(binding.tvWeight));

        damagePaths.add(new PhotoBean());
        damageAdapter = new PhotoAdapter(this, damagePaths, 6, position -> {
            index = 4;
            this.position = position;
            requestCamera("damaged");
        }, str -> {
            index = 4;
        });
        GridLayoutManager damageManager = new GridLayoutManager(this, 3);
        binding.rvDamage.setLayoutManager(damageManager);
        binding.rvDamage.setAdapter(damageAdapter);
    }

    @Override
    protected void initListener() {
        viewModel.tallySkuInfoLiveData.observe(this, data -> {
            skuInfo = data;
            setSkuData(true, skuInfo, skuInfo.isSubmit());
        });
        viewModel.fileUploadMutableLiveData.observe(this, data -> {
            Log.e(TAG, "type():" + data.getType() + ",position:" + data.getPosition());
            switch (data.getType()) {
                case 0:
                    length_path = data.getPath();
                    break;
                case 1:
                    width_path = data.getPath();
                    break;
                case 2:
                    height_path = data.getPath();
                    break;
                case 3:
                    weight_path = data.getPath();
                    break;
                case 4:
                    if (damagePaths.size() > data.getPosition()) {
                        PhotoBean photoBean = damagePaths.get(data.getPosition());
                        photoBean.setUrlImg(data.getPath());
                    }
                    break;
            }
            if (data.isSubmit()) {
                setSubmit();
            }
        });
        viewModel.checkSkuUnitLiveData.observe(this, data -> {
            boolean isLength = false;
            boolean isWidth = false;
            boolean isHeight = false;
            boolean isWeight = false;

            try {
                Double weight = data.getWeight();
                List<Double> sizes = data.getUnitList();
                if (weight != null) {
                    isWeight = true;
                }
                SkuUnitDTO skuUnit = tallyQr.getSkuUnit();
                for (Double d : sizes) {
                    if (d - skuUnit.getLength() == 0) {//
                        isLength = true;
                    }
                    if (d - skuUnit.getWidth() == 0) {//
                        isWidth = true;
                    }
                    if (d - skuUnit.getHeight() == 0) {//
                        isHeight = true;
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
            }

            if ((!isLength) && (!isWidth) && (!isHeight) && (!isWeight)) {
                toTallyLPN();
                return;
            }
            showSkuQTYExceedDialog(isLength, isWidth, isHeight, isWeight);
            showCheckSkuParam(isLength, isWidth, isHeight, isWeight);
        });
        viewModel.errorResultCode.observe(this, code -> {
            showHint((String) code);
        });
        viewModel.isLoading.observe(this, b -> {
            if (b.isShow()) {
                showProgressDialog(b.getMsg());
            } else {
                dismissProgressDialog();
            }
        });
    }

    private void showCheckSkuParam(boolean isLength, boolean isWidth, boolean isHeight, boolean isWeight) {
        binding.tvLength.setTextColor(getResources().getColor(isLength ? R.color.red : R.color.black));
        binding.tvWidth.setTextColor(getResources().getColor(isWidth ? R.color.red : R.color.black));
        binding.tvHeight.setTextColor(getResources().getColor(isHeight ? R.color.red : R.color.black));
        binding.tvWeight.setTextColor(getResources().getColor(isWeight ? R.color.red : R.color.black));
    }

    private void toTallyLPN() {
        Log.e(TAG, "showActivity:");
        tallyQr.setTyp(2);
        Bundle bundle = new Bundle();
        bundle.putSerializable("TallyQr", tallyQr);
        AppRouter.showActivityForResult(mContext, TallyLPNActivity.class, bundle, Constant.ForResult2);
    }

    @Override
    protected void onResume() {
        // TODO Auto-generated method stub
        super.onResume();
        initScan();
    }

    @Override
    protected void onPause() {
        // TODO Auto-generated method stub
        super.onPause();
        unScan();
    }

    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_scan:
                setScan();
                break;
            case R.id.iv_cartons:
                String cartonsQty = binding.tvCartons.getText().toString();
                if (Utils.isEmpty(cartonsQty)) {
                } else if (Integer.parseInt(cartonsQty) > 1) {
                    binding.tvCartons.setText((Integer.parseInt(cartonsQty) - 1) + "");
                } else {
                    binding.tvCartons.setText("0");
                }
                break;
            case R.id.iv_length:
                index = 0;
                setCameraPhotoOrShow(binding.ivLengthDelete, length_path, "length");
                break;
            case R.id.iv_length_delete:
                length_path = "";
                deleteImg(binding.ivLength, binding.ivLengthDelete);

                break;
            case R.id.iv_width:
                index = 1;
                setCameraPhotoOrShow(binding.ivWidthDelete, width_path, "width");
                break;
            case R.id.iv_width_delete:
                width_path = "";
                deleteImg(binding.ivWidth, binding.ivWidthDelete);
                break;
            case R.id.iv_height:
                index = 2;
                setCameraPhotoOrShow(binding.ivHeightDelete, height_path, "height");
                break;
            case R.id.iv_height_delete:
                height_path = "";
                deleteImg(binding.ivHeight, binding.ivHeightDelete);
                break;
            case R.id.iv_weight:
                index = 3;
                setCameraPhotoOrShow(binding.ivWeightDelete, weight_path, "weight");
                break;
            case R.id.iv_weight_delete:
                weight_path = "";
                deleteImg(binding.ivWeight, binding.ivWeightDelete);
                break;
            case R.id.tv_reset:
                skuInfo = null;
                reset();
                break;
            case R.id.tv_next:
                if (System.currentTimeMillis() - timeMillis < 1000) {
                    return;
                }
                timeMillis = System.currentTimeMillis();
                String sku_barcode = binding.tvCode.getText().toString();
                if (Utils.isEmpty(sku_barcode)) {
                    ToastUtil.showToast("Please enter the SKU/Barcode");
                    return;
                }
                if (!isBarCode) {
                    SkuInfoQr skuInfoQr = new SkuInfoQr();
                    skuInfoQr.setBarcode(barcodeStr);
                    skuInfoQr.setTyp(2);
                    skuInfoQr.setOrderId(tally.getOrderId());
                    viewModel.getTallySkuInfo(skuInfoQr, true);
                    return;
                }
                if ((!Utils.isEmpty(barCode) && (!barCode.equals(sku_barcode)))) {
                    SkuInfoQr skuInfoQr = new SkuInfoQr();
                    skuInfoQr.setBarcode(barcodeStr);
                    skuInfoQr.setTyp(2);
                    skuInfoQr.setOrderId(tally.getOrderId());
                    viewModel.getTallySkuInfo(skuInfoQr, true);
                    return;
                }
                setSubmit();
                break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == Constant.ForResult2 && resultCode == RESULT_OK) {
            Log.e(TAG, "onActivityResult");
            handler.sendEmptyMessage(0);
        }
    }

    Handler handler = new Handler() {
        @Override
        public void handleMessage(@NonNull Message msg) {
            super.handleMessage(msg);
            skuInfo = null;
            reset();
        }
    };

    @Override
    protected void getImgPath(String path) {
        setPath(index, path);
    }

    private TextView.OnEditorActionListener EnterListenter = new TextView.OnEditorActionListener() {

        @Override
        public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
            if (actionId == EditorInfo.IME_ACTION_SEARCH) {
                barcodeStr = binding.tvCode.getText().toString();
                binding.tvCode.setText(barcodeStr);
                showHint("");
                SkuInfoQr skuInfoQr = new SkuInfoQr();
                skuInfoQr.setBarcode(barcodeStr);
                skuInfoQr.setTyp(2);
                skuInfoQr.setOrderId(tally.getOrderId());
                viewModel.getTallySkuInfo(skuInfoQr, false);
            }
            return false;
        }
    };

    private View.OnFocusChangeListener onFocusChangeListener = new View.OnFocusChangeListener() {
        @Override
        public void onFocusChange(View v, boolean hasFocus) {
            if (!hasFocus) {
                type = 0;
                return;
            }

            switch (v.getId()) {
                case R.id.tv_code:
                    type = 0;
                    break;
                case R.id.tv_cartons:
                    type = 1;
                    break;
            }
        }
    };

    @Override
    public void getScanninCode() {
        super.getScanninCode();
        bottomLayoutBinding.tvHint.setText("");
        judgeSku();
    }

    private void judgeSku() {
        new AsyncTask<String, Integer, Integer>() {

            @Override
            protected Integer doInBackground(String... strings) {
                switch (type) {
                    case 0:
                        if (skuInfo != null) {
                            if (barcodeStr.equalsIgnoreCase(skuInfo.getSku())) {
                                return 4;//扫描的是同一个SKU
                            }
                            List<String> barcodes = skuInfo.getBarcodeList();
                            if (barcodes != null) {
                                for (String barcode : barcodes) {
                                    if (barcodeStr.equalsIgnoreCase(barcode)) {//扫描的是barcode
                                        return 4;//扫描的是同一个barcode
                                    }
                                }
                            }
                        }
                        return 6;
                    case 1:
                        if (skuInfo != null) {
                            if (barcodeStr.equalsIgnoreCase(skuInfo.getSku())) {
                                return type;
                            }
                            List<String> barcodes = skuInfo.getBarcodeList();
                            if (barcodes != null) {
                                for (String barcode : barcodes) {
                                    if (barcodeStr.equalsIgnoreCase(barcode)) {//扫描的是barcode
                                        return type;
                                    }
                                }
                            }
                        }
                        return 5;

                }
                return -1;
            }

            @Override
            protected void onPostExecute(Integer i) {
                super.onPostExecute(i);
                switch (i) {
                    case 0:
                        reset();
                        binding.tvCode.setText(barcodeStr);
                        setSkuData(true, skuInfo, false);
                        break;
                    case 1:
                        String ctn = binding.tvCartons.getText().toString();
                        if (Utils.isEmpty(ctn)) {
                            binding.tvCartons.setText(1 + "");
                        } else {
                            binding.tvCartons.setText((1 + Integer.parseInt(ctn)) + "");
                        }
                        break;
                    case 4:
                        binding.tvCode.setText(barcodeStr);
                        break;
                    case 5:
                        mVibrator.vibrate(new long[]{0, 1000}, -1);
                        break;
                    case 6:
                        reset();
                        binding.tvCode.setText(barcodeStr);
                        SkuInfoQr skuInfoQr = new SkuInfoQr();
                        skuInfoQr.setBarcode(barcodeStr);
                        skuInfoQr.setTyp(2);
                        skuInfoQr.setOrderId(tally.getOrderId());
                        viewModel.getTallySkuInfo(skuInfoQr, false);
                        break;
                }

            }
        }.execute();
    }


    private void setRemove(int type) {
        switch (type) {
            case 0:
                length_path = "";
                deleteImg(binding.ivLength, binding.ivLengthDelete);
                break;
            case 1:
                width_path = "";
                deleteImg(binding.ivWidth, binding.ivWidthDelete);
                break;
            case 2:
                height_path = "";
                deleteImg(binding.ivHeight, binding.ivHeightDelete);
                break;
            case 3:
                weight_path = "";
                deleteImg(binding.ivWeight, binding.ivWeightDelete);
                break;
        }
    }

    private void setSkuData(boolean isOk, SkuInfo skuUpType, boolean isSubmit) {
        Log.e(TAG, "setSkuData");
        isBarCode = isOk;
        if (skuUpType == null) {
            return;
        }
        barCode = "";
        sku = "";
        if (isBarCode) {
            barCode = binding.tvCode.getText().toString();
            this.sku = skuUpType.getSku();
        }

        binding.tvCodeSku.setText(sku + "");
        binding.tvCartons.setText("1");
        binding.tvCartons.requestFocus();
        SkuUnitDTO skuBaseRespVo = skuUpType.getSkuUnit();

        if (skuBaseRespVo != null) {
            Log.e(TAG, "skuBaseRespVo.getLength():" + skuBaseRespVo.getLength());
            if (skuBaseRespVo.getLength() > 0) {
                binding.tvLength.setText(skuBaseRespVo.getLength() + "");
                EditTextUtil.setSelection(binding.tvLength);
            }
            if (skuBaseRespVo.getWidth() > 0) {
                binding.tvWidth.setText(skuBaseRespVo.getWidth() + "");
                EditTextUtil.setSelection(binding.tvWidth);
            }
            if (skuBaseRespVo.getHeight() > 0) {
                binding.tvHeight.setText(skuBaseRespVo.getHeight() + "");
                EditTextUtil.setSelection(binding.tvHeight);
            }
            if (skuBaseRespVo.getWeight() > 0) {
                binding.tvWeight.setText(skuBaseRespVo.getWeight() + "");
                EditTextUtil.setSelection(binding.tvWeight);
            }
        }
        List<FilePathListDTO> filePathList = skuUpType.getFilePathList();
        if (filePathList != null) {
            for (FilePathListDTO filePathListDTO : filePathList) {
                if (filePathListDTO.getPathList() != null && filePathListDTO.getPathList().size() > 0) {
                    if (filePathListDTO.getName().contains("length")) {
                        length_path = filePathListDTO.getPathList().get(0);
                        setShow(length_path, binding.ivLength, binding.ivLengthDelete);
                    } else if (filePathListDTO.getName().contains("width")) {
                        width_path = filePathListDTO.getPathList().get(0);
                        setShow(width_path, binding.ivWidth, binding.ivWidthDelete);
                    } else if (filePathListDTO.getName().contains("height")) {
                        height_path = filePathListDTO.getPathList().get(0);
                        setShow(height_path, binding.ivHeight, binding.ivHeightDelete);
                    } else if (filePathListDTO.getName().contains("weight")) {
                        weight_path = filePathListDTO.getPathList().get(0);
                        setShow(weight_path, binding.ivWeight, binding.ivWeightDelete);
                    } else if (filePathListDTO.getName().equals("damaged")) {
                        List<String> pathList = filePathListDTO.getPathList();
                        damagePaths.clear();
                        for (String str : pathList) {
                            PhotoBean imgUrl = new PhotoBean();
                            imgUrl.setPathImg(str);
                            imgUrl.setUrlImg(str);
                            damagePaths.add(position, imgUrl);
                        }
                        if (damagePaths.size() < 6) {
                            damagePaths.add(new PhotoBean());
                        }
                        Log.e(TAG, "damagePaths.size():" + damagePaths.size());
                        damageAdapter.notifyDataSetChanged();
                    }
                }

            }
        }

        if (isOk && isSubmit) {
            setSubmit();
        }
    }

    private void reset() {
        binding.tvCode.setText("");
        binding.tvCode.requestFocus();
        binding.tvCartons.setText("");
        binding.tvLength.setText("");
        binding.tvWidth.setText("");
        binding.tvHeight.setText("");
        binding.tvWeight.setText("");
        binding.tvCodeSku.setText("");

        length_path = "";
        deleteImg(binding.ivLength, binding.ivLengthDelete);
        width_path = "";
        deleteImg(binding.ivWidth, binding.ivWidthDelete);
        height_path = "";
        deleteImg(binding.ivHeight, binding.ivHeightDelete);
        weight_path = "";
        deleteImg(binding.ivWeight, binding.ivWeightDelete);

        binding.tvCode.requestFocus();
        binding.tvLength.setTextColor(getResources().getColor(R.color.black));
        binding.tvWidth.setTextColor(getResources().getColor(R.color.black));
        binding.tvHeight.setTextColor(getResources().getColor(R.color.black));
        binding.tvWeight.setTextColor(getResources().getColor(R.color.black));

        damagePaths.clear();
        damagePaths.add(new PhotoBean());
        damageAdapter.notifyDataSetChanged();
        Log.e(TAG, "reset");
    }

    private void setSubmit() {
        Log.e(TAG, "setSubmit");
        String sku_barcode = binding.tvCode.getText().toString();
        if (Utils.isEmpty(sku_barcode)) {
            ToastUtil.showToast("Please enter the SKU/Barcode");
            return;
        }
        String cartons = binding.tvCartons.getText().toString();
        if (Utils.isEmpty(cartons) ) {
            ToastUtil.showToast("Scan +1");
            return;
        }
        if (Integer.parseInt(cartons) < 1) {
            ToastUtil.showToast("Scan +1");
            return;
        }

//        String length = binding.tvLength.getText().toString();
//        if (!judgeType(length, "Please enter the length")) {
//            return;
//        }
//        String width = binding.tvWidth.getText().toString();
//        if (!judgeType(width, "Please enter the width")) {
//            return;
//        }
//        String height = binding.tvHeight.getText().toString();
//        if (!judgeType(height, "Please enter the height")) {
//            return;
//        }
//        String weight = binding.tvWeight.getText().toString();
//        if (!judgeType(weight, "Please enter the weight")) {
//            return;
//        }

        tallyQr = new TallyQr();
        tallyQr.setContainerNo(tally.getContainerNo());
        tallyQr.setOrderId(tally.getOrderId());
        tallyQr.setBarcode(sku_barcode);
        tallyQr.setQty(Integer.parseInt(cartons));

//        tallyQr.setQty(1);
        tallyQr.setPutawayType(skuInfo.getPutawayType());
        Log.e(TAG, "length_path:" + length_path);
        Log.e(TAG, "width_path:" + width_path);
        Log.e(TAG, "height_path:" + height_path);
        Log.e(TAG, "weight_path:" + weight_path);
        try {
//            if (Utils.isEmpty(length_path)) {
//                ToastUtil.showToast("Please add length photo");
//                return;
//            }
//            if (Utils.isEmpty(width_path)) {
//                ToastUtil.showToast("Please add width photo");
//                return;
//            }
//            if (Utils.isEmpty(height_path)) {
//                ToastUtil.showToast("Please add height photo");
//                return;
//            }
//            if (Utils.isEmpty(weight_path)) {
//                ToastUtil.showToast("Please add weight photo");
//                return;
//            }
            if (Utils.isEmpty(damagePaths.get(0).getPathImg())) {
                ToastUtil.showToast("Please add damaged picture");
                return;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

//        if (!Utils.isEmpty(length_path)) {
//            if (!length_path.contains("http")) {
//                viewModel.fileUpload(tally.getContainerNo(), "length", length_path, 0, 0, true, true);
//                return;
//            }
//        }
//        if (!Utils.isEmpty(width_path)) {
//            if (!width_path.contains("http")) {
//                viewModel.fileUpload(tally.getContainerNo(), "width", width_path, 1, 0, true, true);
//                return;
//            }
//        }
//        if (!Utils.isEmpty(height_path)) {
//            if (!height_path.contains("http")) {
//                viewModel.fileUpload(tally.getContainerNo(), "height", height_path, 2, 0, true, true);
//                return;
//            }
//        }
//        if (!Utils.isEmpty(weight_path)) {
//            if (!weight_path.contains("http")) {
//                viewModel.fileUpload(tally.getContainerNo(), "weight", weight_path, 3, 0, true, true);
//                return;
//            }
//        }
        List<String> damagedPathList = new ArrayList<>();
        if (damagePaths.size() > 0) {
            for (int i = 0; i < damagePaths.size(); i++) {
                PhotoBean photoBean = damagePaths.get(i);
                String s = photoBean.getUrlImg();
                Log.e(TAG, "s:" + photoBean.toString());
                if (!Utils.isEmpty(s)) {
                    if (!s.contains("http")) {
                        position = i;
                        viewModel.fileUpload(binding.tvCode.getText().toString(), "damaged", s, 4, position, true, true);
                        return;
                    }
                    damagedPathList.add(s);
                } else if (!Utils.isEmpty(photoBean.getPathImg())) {
                    position = i;
                    viewModel.fileUpload(binding.tvCode.getText().toString(), "damaged", photoBean.getPathImg(), 4, position, true, true);
                    return;
                }
            }
        }
        Log.e(TAG, "tallyPhotoFlag:");
//        SkuUnitDTO skuUnit = tallyQr.getSkuUnit();
//        if (skuUnit == null) {
//            skuUnit = new SkuUnitDTO();
//        }
//        skuUnit.setWeight(Double.parseDouble(weight));
//        skuUnit.setLength(Double.parseDouble(length));
//        skuUnit.setWidth(Double.parseDouble(width));
//        skuUnit.setHeight(Double.parseDouble(height));
//        tallyQr.setSkuUnit(skuUnit);

        if (skuInfo != null) {
            tallyQr.setSkuUnit(skuInfo.getSkuUnit());
        }

        List<FilePathListDTO> filePathList = new ArrayList<>();

//        FilePathListDTO weightFilePathListDTO = new FilePathListDTO();
//        List<String> weightPathList = new ArrayList<>();
//        weightPathList.add(weight_path);
//        weightFilePathListDTO.setName("weight");
//        weightFilePathListDTO.setPathList(weightPathList);
//        filePathList.add(weightFilePathListDTO);
//
//        FilePathListDTO lengthFilePathListDTO = new FilePathListDTO();
//        List<String> lengthPathList = new ArrayList<>();
//        lengthPathList.add(length_path);
//        lengthFilePathListDTO.setName("length");
//        lengthFilePathListDTO.setPathList(lengthPathList);
//        filePathList.add(lengthFilePathListDTO);
//
//        FilePathListDTO widthFilePathListDTO = new FilePathListDTO();
//        List<String> widthPathList = new ArrayList<>();
//        widthPathList.add(width_path);
//        widthFilePathListDTO.setName("width");
//        widthFilePathListDTO.setPathList(widthPathList);
//        filePathList.add(widthFilePathListDTO);
//
//        FilePathListDTO heightFilePathListDTO = new FilePathListDTO();
//        List<String> heightPathList = new ArrayList<>();
//        heightPathList.add(height_path);
//        heightFilePathListDTO.setName("height");
//        heightFilePathListDTO.setPathList(heightPathList);
//        filePathList.add(heightFilePathListDTO);

        FilePathListDTO damagedFilePathListDTO = new FilePathListDTO();
        damagedFilePathListDTO.setName("damaged");
        damagedFilePathListDTO.setPathList(damagedPathList);
        filePathList.add(damagedFilePathListDTO);

//        CheckSkuUnit checkSkuParam = new CheckSkuUnit();
//        checkSkuParam.setOrderId(tally.getOrderId());
//        checkSkuParam.setBarcode(sku_barcode);
//        List<Double> sizes = checkSkuParam.getUnitList();
//        sizes.add(skuUnit.getLength());
//        sizes.add(skuUnit.getWidth());
//        sizes.add(skuUnit.getHeight());

//        // 使用Collections.sort方法对列表进行排序
//        Collections.sort(sizes);
//        Collections.reverse(sizes);
//
//        checkSkuParam.setWeight(skuUnit.getWeight());

        tallyQr.setFilePathList(filePathList);
        tallyQr.setTyp(2);
        Log.e(TAG, "tallyQr.toString():" + tallyQr.toString());
        toTallyLPN();
    }

    private boolean judgeType(String mold, String hint) {
        if (Utils.isEmpty(mold)) {
            ToastUtil.showToast(hint);
            return false;
        }

        if (Double.parseDouble(mold) <= 0) {
            ToastUtil.showToast(hint);
            return false;
        }
        return true;
    }

    //=================================================
    private void setCameraPhotoOrShow(ImageView iv_delete, String path, String name) {
        temp_path = path;
        if (iv_delete.getVisibility() == View.VISIBLE) {
            showPhotoDialog(temp_path);
        } else {
            requestCamera(name);
        }
    }

    private void setPath(int type, String pathImage) {
        Log.e(TAG, "setPath>type:" + type + ",pathImage:" + pathImage);
        if (!pathImage.contains("http")) {//选择的图片
            String fileName = "";
            switch (type) {
                case 0:
                    fileName = "length";
                    break;
                case 1:
                    fileName = "width";
                    break;
                case 2:
                    fileName = "height";
                    break;
                case 3:
                    fileName = "weight";
                    break;
                case 4:
                    fileName = "damaged";
                    break;
            }
            viewModel.fileUpload(tally.getContainerNo(), fileName, pathImage, type, position, false, false);
        }
        switch (type) {
            case 0:
                length_path = pathImage;
                setShow(length_path, binding.ivLength, binding.ivLengthDelete);
                break;
            case 1:
                width_path = pathImage;
                setShow(width_path, binding.ivWidth, binding.ivWidthDelete);
                break;
            case 2:
                height_path = pathImage;
                setShow(height_path, binding.ivHeight, binding.ivHeightDelete);
                break;
            case 3:
                weight_path = pathImage;
                setShow(weight_path, binding.ivWeight, binding.ivWeightDelete);
                break;
            case 4:
                PhotoBean imgUrl = new PhotoBean();
                imgUrl.setPathImg(pathImage);
                imgUrl.setUrlImg(pathImage);
                if (damagePaths.size() > position) {
                    damagePaths.remove(position);
                    damagePaths.add(position, imgUrl);
                }
                if (damagePaths.size() < 6) {
                    if (!Utils.isEmpty(damagePaths.get(damagePaths.size() - 1).getPathImg())) {
                        damagePaths.add(new PhotoBean());
                    }
                }
                for (PhotoBean img : damagePaths) {
                    Log.e(TAG, img.toString());
                }
                if (damageAdapter != null) {
                    damageAdapter.setData(damagePaths);
                }
                break;
        }
    }

    private AlertDialog dialogSkuQTYExceed;

    private void showSkuQTYExceedDialog(boolean isLength, boolean isWidth, boolean isHeight, boolean isWeight) {
        dialogSkuQTYExceed = MyDialogUtil.getDialog(mContext, initSkuQTYExceedDialogView(isLength, isWidth, isHeight, isWeight), Gravity.CENTER);
        dialogSkuQTYExceed.setCanceledOnTouchOutside(true);
        dialogSkuQTYExceed.show();
    }

    private View initSkuQTYExceedDialogView(boolean isLength, boolean isWidth, boolean isHeight, boolean isWeight) {
        DialogTallyCheckskuBinding dialogBinding = DialogTallyCheckskuBinding.inflate(LayoutInflater.from(mContext));
        String content = (isLength ? "length " : "") + (isWidth ? "width " : "") + (isHeight ? "height " : "") + (isWeight ? "weight " : "");
        dialogBinding.tvContent.setText(content);
        dialogBinding.tvCancel.setOnClickListener(v -> {
            if (dialogSkuQTYExceed != null) {
                dialogSkuQTYExceed.dismiss();
            }
        });
        dialogBinding.tvConfirm.setOnClickListener(v -> {

            if (dialogSkuQTYExceed != null) {
                dialogSkuQTYExceed.dismiss();
            }
            toTallyLPN();
        });
        return dialogBinding.getRoot();
    }
}
